import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    a: 100,
    student: [
      {
        id: 0,
        name: 'tom123',
        pass: 'tom123',
        age: 19
      },
      {
        id: 1,
        name: 'sam123',
        pass: 'sam123',
        age: 18
      },
      {
        id: 2,
        name: 'wenddy',
        pass: 'wenddy',
        age: 17
      }
    ]
  },
  mutations: {
    //增加state.count的函数
    add(state, n) {
      state.a += n
    },
    //修改用户的密码
    updatePass(state, option) {
      state.student.forEach(item => {
        item.pass = option.pass
        console.log(option.num)
      })
    },
    sub(state, n) {
      state.a -= n
    }
  },
  getters: {
    //获得数组的长度
    getStudentLength(state) {
      return state.student.length
    },
    //获得age>17的学生
    getStuGt17(state) {
      return state.student.filter(item => item.age > 17)
    }
  },
  actions: {
    addA(ct) {
      ct.commit('add', 10)
    },
    subA({ commit }) {
      commit('sub', 100)
    }
  },
  modules: {}
})

export default store
